package com.luyuan.crm.sale.web;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.faces.model.DataModel;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.luyuan.crm.sale.model.Order;
import com.luyuan.crm.sale.model.OrderStatus;
import com.luyuan.crm.sale.service.OrderService;
import com.ruijie.web.jsf.action.BaseAction;
import com.ruijie.web.jsf.context.TableContext;
import com.ruijie.web.jsf.model.PageDataModel;
import com.ruijie.web.jsf.model.TableOrder;
import com.ruijie.web.jsf.model.TablePage;
import com.ruijie.foundation.query.Page;
import com.ruijie.foundation.query.criteria.CriteriaTemplate;
import com.ruijie.foundation.query.criteria.RestrictionTemplate;
import com.ruijie.util.StringUtils;

@Component("orderSupportManagerAction")
@Scope("view")
public class OrderSupportManagerAction extends BaseAction implements InitializingBean {

    @Resource(name = "orderService")
    private OrderService orderService;

    private Order order;

    private String uuid;

    public void appointSupport() {
        orderService.appointSupport(order, order.getSupport());
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        String id = getRequestParameter("uuid");
        if (StringUtils.isNotEmpty(id)) {
            order = orderService.getById(id);
        }

    }

    /**
     * 退货状态的列表
     * 
     * @return
     */
    public DataModel getOrderTableModel() {
        TablePage tablePage = TableContext.getTablePage();
        List<TableOrder> tableOrders = TableContext.getTableOrders();
        List<org.hibernate.criterion.Order> orders = new ArrayList<org.hibernate.criterion.Order>();
        for (TableOrder tableOrder : tableOrders) {
            org.hibernate.criterion.Order order = tableOrder.toOrder();
            if (order != null) {
                orders.add(order);
            }
        }

        CriteriaTemplate c = CriteriaTemplate.forClass(Order.class);
        RestrictionTemplate r = new RestrictionTemplate(false);
        c.add(r.eq("orderStatus", OrderStatus.REJECTED));
        if (StringUtils.isNotEmpty(uuid)) {
            c.add(r.eq("id", uuid));
        }

        Page page = orderService.pagedQuery(c, tablePage.getPage(), tablePage.getPageSize(), orders);
        return new PageDataModel(page);
    }

    public String getUuid() {
        return uuid;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }

    public Order getOrder() {
        return order;
    }

    public void setOrder(Order order) {
        this.order = order;
    }
}
